package com.woniu.adapter;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.outlet.dao.MaterialClassificationTypePoMapper;
import com.woniu.outlet.po.MaterialClassificationTypePo;
import com.woniu.outlet.rabbitmq.MessageRepo;
import com.woniu.outlet.redis.RedisRepo;
import com.woniu.utils.SnowflakeIdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class MaterialClassificationTypeAdapter {

    @Autowired
    private MaterialClassificationTypePoMapper materialClassificationTypePoMapper;

    @Autowired
    private RedisRepo redisRepo;

    @Autowired
    private MessageRepo messageRepo;


    public List<MaterialClassificationTypePo> findMaterialClassificationTypes() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String str = redisRepo.findRedisRepo("materialClassification:list");
        List<MaterialClassificationTypePo> lists = new ArrayList<>();
        if (!StringUtils.isEmpty(str)) {
            lists = mapper.readValue(str, new TypeReference<List<MaterialClassificationTypePo>>() {
            });
            return lists;
        }
        lists = materialClassificationTypePoMapper.selectByExample(null);
        //存到redis
        redisRepo.saveRedisRepo("materialClassificationType:list",mapper.writeValueAsString(lists));
        return lists;
    }

    public MaterialClassificationTypePo getMaterialClassificationTypeById(Long id) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String str = redisRepo.findRedisRepo("materialClassificationType:" + id);
        MaterialClassificationTypePo materialClassificationTypePo = null;
        if (!StringUtils.isEmpty(str)) {
            materialClassificationTypePo= mapper.readValue(str, new TypeReference<MaterialClassificationTypePo>() {
            });
            return materialClassificationTypePo;
        }
        materialClassificationTypePo = materialClassificationTypePoMapper.selectByPrimaryKey(id);
        //存到redis
        redisRepo.saveRedisRepo("materialClassificationType:"+id,mapper.writeValueAsString(materialClassificationTypePo));
        return materialClassificationTypePo;
    }

    public void addMaterialClassificationTypePo(MaterialClassificationTypePo materialClassificationTypePo){
        SnowflakeIdGenerator snowflakeIdGenerator = new SnowflakeIdGenerator();
        long id = snowflakeIdGenerator.nextId();
        materialClassificationTypePo.setClassificationCode("FLBM"+id);

        materialClassificationTypePoMapper.insert(materialClassificationTypePo);
        redisRepo.deleteRedisRepo("materialClassificationType:list");

    }

    public void updateMaterialClassificationType(MaterialClassificationTypePo materialClassificationType){
        materialClassificationTypePoMapper.updateByPrimaryKeySelective(materialClassificationType);
        redisRepo.deleteRedisRepo("materialClassificationType:list");
    }

    public void deleteMaterialClassificationTypePoById(Long id){
        materialClassificationTypePoMapper.deleteByPrimaryKey(id);
        redisRepo.deleteRedisRepo("materialClassificationType:list");
    }
}
